package com.ddw.meedeeuh.presenters;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.ddw.meedeeuh.MeedeeuhApp;
import com.ddw.meedeeuh.presenters.MovieEditorPresenter.MovieEditorView;
import com.ddw.meedeeuh.shared.events.OpenMovieEditorEvent;
import com.ddw.meedeeuh.shared.events.OpenMovieEditorEventHandler;
import com.ddw.meedeeuh.shared.mvp.PresenterBase;
import com.ddw.meedeeuh.shared.mvp.View;

public class EditorContainerPresenter extends PresenterBase<EditorContainerPresenter.EditorContainerView> {

	public EditorContainerPresenter(MeedeeuhApp app, EditorContainerView view) {
		super(app, view);
	}

	public static interface EditorContainerView extends View {

		public void addEditor(MovieEditorView editor);

		public boolean containsEditor(MovieEditorView editor);

		public void setSelectedEditor(MovieEditorView editor);

		public void removeEditor(MovieEditorView editor);

		public MovieEditorView getSelectedEditor();

		public void addEditorChangeListener(ChangeListener listener);

		public void setToolbarEnabled(boolean enabled);

	}

	@Override
	public void onSetup() {
		// Listen to open movie editor events
		getApp().getEventBus().register(new OpenMovieEditorEventHandler() {

			@Override
			public void onOpenMovieEditor(OpenMovieEditorEvent e) {
				if(getView().containsEditor(e.getView()) == false) {
					getView().addEditor(e.getView());
				}
				getView().setSelectedEditor(e.getView());
			}

		});

		// Listen to tab changes
		getView().addEditorChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				final MovieEditorView editor = getView().getSelectedEditor();

				if(editor == null) {
					getView().setToolbarEnabled(false);
				} else if(editor instanceof MovieEditorView) {
					getView().setToolbarEnabled(true);
					// TODO: enable or disable toolbar items
				}
			}

		});
	}

}
